﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.DomainServices.Client;
using System.Windows;
using System.Windows.Media;
using NetBay.Core.BusinessEntities;
using Rio.Applications.Web.Services;

namespace Rio.Framework.Pages.CriteriaControls
{

    public partial class ListUserIndexControl
    {

        #region Attributs

        private List<User> m_oListUser;
        private string m_lSelectedUserFullName;

        #endregion

        #region Properties

        /// <summary>
        /// Get or set the control value
        /// </summary>
        public override string InputValue
        {
            get
            {
                if (ComboboxInput.SelectedItem != null)
                {
                    //todo: Don't know why ";" is put at the start and he end of the value, it's like this in the existance base
                    return ";" + ((User)ComboboxInput.SelectedItem).FullName.ToString() + ";";
                }
                return String.Empty;
            }
            set
            {
                if (value != null)
                {
                    if (m_oListUser.Where(e => e.FullName.ToString() == value).FirstOrDefault() != null)
                    {
                        ComboboxInput.SelectedItem = m_oListUser.Where(e => e.FullName.ToString() == value).First();
                    }
                    else
                    {
                        ComboboxInput.SelectedIndex = -1;
                    }
                }
            }
        }

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ListUserIndexControl"/> class.
        /// </summary>
        public ListUserIndexControl()
        {
            InitializeComponent();

            _originalBorderBrush = this.ComboboxInput.BorderBrush as SolidColorBrush;
        }


        /// <summary>
        /// Create an instance of the control
        /// </summary>
        /// <param name="index"><see cref="Index"/> instance</param>
        /// <param name="value">Default value</param>
        public ListUserIndexControl(Index index, string value = null)
            : this()
        {
            CurrentIndex = index;
            LabelControl.Text = CurrentIndex.IndexLabel;

            //if a user is selected at the start
            m_lSelectedUserFullName = value;

            //Mandatory indication
            LabelMandatory.Visibility = CurrentIndex.IsMandatory ? Visibility.Visible : Visibility.Collapsed;

            m_oListUser = new List<User>();

            //Load user list
            BusinessEntitiesDomainContext l_oBEDomainContext = new BusinessEntitiesDomainContext();

            var query = l_oBEDomainContext.GetAllUsersQuery();
            l_oBEDomainContext.Load(query, OnGetAllUsersQueryCallback, null);

            FilterButton.Visibility = Visibility.Visible;
        }

        #endregion

        /// <summary>
        /// Called when [get all users query callback].
        /// </summary>
        /// <param name="result">The result.</param>
        private void OnGetAllUsersQueryCallback(LoadOperation<User> result)
        {

            m_oListUser.AddRange(result.Entities);
            ComboboxInput.ItemsSource = m_oListUser;
            ComboboxInput.DisplayMemberPath = "FullName";

            //When items are filled, we selected the item (it used when a search by uri)
            if (!String.IsNullOrWhiteSpace(SelectedValueAfterInit))
            {
                ComboboxInput.SelectedItem = ComboboxInput.Items.Cast<User>().Where(item => item.FullName == SelectedValueAfterInit).FirstOrDefault();
            }

            //if a user is selected at the start
            if (m_lSelectedUserFullName != null)
            {
                User l_oSelectedUser = m_oListUser.Where(e => e.FullName.Equals(m_lSelectedUserFullName)).FirstOrDefault();
                if (l_oSelectedUser != null)
                {
                    ComboboxInput.SelectedItem = l_oSelectedUser;
                }
                else
                {
                    ComboboxInput.SelectedIndex = -1;
                }
            }
        }

        /// <summary>
        /// Display filter window with a list of values input
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonInput_Click(object sender, RoutedEventArgs e)
        {
            OnFilterClick(new Events.FilterClickEventArgs(this.CurrentIndex));
        }

        /// <summary>
        /// Handles the Click event of the buttonDelete control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void buttonDelete_Click(object sender, RoutedEventArgs e)
        {
            OnRemoveControlClick(new EventArgs());
        }

        /// <summary>
        /// Clear value
        /// </summary>
        public override void Clear()
        {
            ComboboxInput.SelectedIndex = -1;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ComboboxInput_LostFocus(object sender, RoutedEventArgs e)
        {
            OnValueValidated(EventArgs.Empty);
        }

        /// <summary>
        /// Used to set border color of input control
        /// </summary>
        public override void ChangeBorderColor(Color color)
        {
            this.ComboboxInput.BorderBrush = new SolidColorBrush(color);
        }

        public override void RevertBorderColor()
        {
            this.ComboboxInput.BorderBrush = _originalBorderBrush;
        }

        /// <summary>
        /// Set the max lenght of the control
        /// </summary>
        public override void SetMaxLength()
        {
        }

        /// <summary>
        /// Hide mandatory label
        /// </summary>
        public override void DisableMandatoryLabel()
        {
            this.LabelMandatory.Visibility = Visibility.Collapsed;
        }

        /// <summary>
        /// Hides Index label name
        /// </summary>
        public override void DisableIndexLabel()
        {
            this.LabelControl.Text = string.Empty;
        }

        /// <summary>
        /// Activate Removing feature
        /// </summary>
        public override void ActiveRemove()
        {
            this.DeleteButton.Visibility = Visibility.Visible;
        }

        /// <summary>
        /// Hides filter button
        /// </summary>
        public override void DisableFilter()
        {
            this.FilterButton.Visibility = Visibility.Collapsed;
        }

    }

}
